<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-row :gutter="40">
        <el-col :span="20">
          <el-form-item label="影响范围" prop="coverageInfluence">
            <el-select v-model="formItem.coverageInfluence" class="select_input" placeholder="影响范围">
              <el-option v-for="(item) in coverageInfluences" :key="item.code" :label="item.text" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="20">
          <el-form-item label="紧急程度" prop="urgency">
            <el-select v-model="formItem.urgency" class="select_input" placeholder="紧急程度">
              <el-option v-for="(item) in urgencys" :key="item.code" :label="item.text" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="20">
          <el-form-item label="优先级" prop="priority">
            <el-select v-model="formItem.priority" class="select_input" placeholder="优先级">
              <el-option v-for="(item) in prioritys" :key="item.code" :label="item.text" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {
  create,
  update,
  detail
} from '@neu/dcom-ui/api/priority'
import { findByDictionaryType } from '@neu/dcom-ui/api/dictionary'
const formItem = {
  coverageInfluence: '',
  urgency: '',
  priority: ''
}
export default {
  name: 'PriorityEditForm',
  components: {
  },
  props: {
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      /** 影响范围 */
      coverageInfluences: [],
      /** 紧急程度 */
      urgencys: [],
      /** 优先级 */
      prioritys: [],
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        coverageInfluence: [{
          required: true,
          message: '影响范围不能为空',
          trigger: 'blur'
        }],
        urgency: [{
          required: true,
          message: '紧急程度不能为空',
          trigger: 'blur'
        }],
        priority: [{
          required: true,
          message: '优先级不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  watch: {
    id: function() {
      this.fetch()
    }
  },
  mounted() {
    this.fetch()
    findByDictionaryType('coverageInfluence').then(response => {
      this.coverageInfluences = response.data
    })
    findByDictionaryType('urgency').then(response => {
      this.urgencys = response.data
    })
    findByDictionaryType('priority').then(response => {
      this.prioritys = response.data
    })
  },
  methods: {
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
      this.formItem = { ...formItem }
    }
  }
}
</script>
